package five;

public class StudentTest {
    public static void main(String[] args) {
        student[] arr = new student[3];
        student stu1 = new student(1, "张三", 23);
        student stu2 = new student(2, "李四", 24);
        student stu3 = new student(3, "王五", 25);
        student stu4 = new student(4, "赵六", 26);
        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;


        boolean flag = contains(arr, stu4.getId());
        if (flag) {
            System.out.println("当前id重复");
        } else {
            int count = getCount(arr);
            if (count == arr.length) {
                student[] NewArr = newArr(arr);
                NewArr[count] = stu4;
                printArr(NewArr);
            } else {
                arr[count] = stu4;
                printArr(arr);
            }

        }

        int Index = getIndex(arr, 1);
        if (Index >= 0) {
            arr[Index] = null;
            printArr(arr);
        } else {
            System.out.println("id不存在");
        }


        int index = getIndex(arr, 2);
        if (index >= 0) {
            student stu = arr[index];
            int newAge = stu.getAge() + 1;
            stu.setAge(newAge);
            printArr(arr);

        }

    }


    public static int getIndex(student[] arr, int id) {
        for (int i = 0; i < arr.length; i++) {
            student stu = arr[i];
            if (stu != null) {
                int sid = stu.getId();
                if (sid == id) {
                    return i;
                }
            }

        }

        return -1;

    }

    public static void printArr(student[] arr) {
        for (int i = 0; i < arr.length; i++) {
            student stu = arr[i];
            if (stu != null) {
                System.out.println(stu.getId() + "," + stu.getName() + "," + stu.getAge());
            }
        }

    }


    public static student[] newArr(student[] arr) {
        student[] newArr = new student[arr.length + 1];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        return newArr;

    }


    public static int getCount(student[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                count++;
            }
        }
        return count;
    }


    public static boolean contains(student[] arr, int id) {
        for (int i = 0; i < arr.length; i++) {
            student stu = arr[i];
            if (stu != null) {
                int sid = stu.getId();
                if (sid == id) {
                    return true;
                }
            }
        }
        return false;
    }
}
